package com.gxitsky.utils;

import cn.hutool.core.lang.Assert;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Random;
import java.util.UUID;

public class UidUtil {

    private static final int RANDOM_LENGTH = 2;
    private static Long MILLIS = System.currentTimeMillis();
    private static Long UID = 0L;

    /**
     * 毫秒值(13位)+3位随机数
     *
     * @return String
     */
    public static String millisIdStr() {
        long millis = System.currentTimeMillis();
        Random random = new Random();
        int anInt = random.nextInt(999);
        return millis + String.format("%03d", anInt);
    }

    /**
     * @desc 唯一id
     * @params length
     * @author gxing
     * @date 2021/9/25 23:33
     */
    public static synchronized Long getUid() {
        return getUid(RANDOM_LENGTH);
    }

    /**
     * @desc 唯一id, 指定随机数长度
     * @params length
     * @author gxing
     * @date 2021/9/25 23:33
     */
    public static synchronized Long getUid(int length) {
        long millis = System.currentTimeMillis();
        int length1 = String.valueOf(UID).length();
        int length2 = String.valueOf(millis).length() + length + 1;
        if (length1 != length2) {
            MILLIS = millis;
            String uid = millis + "0" + RandomStringUtils.randomNumeric(length);
            UID = Long.valueOf(uid);
            return UID;
        }
        if (millis == MILLIS) {
            UID++;
        } else {
            MILLIS = millis;
            String uid = millis + "0" + RandomStringUtils.randomNumeric(length);
            UID = Long.valueOf(uid);
        }
        return UID;
    }

    /**
     * 转长整型
     *
     * @return
     */
    public static Long millisIdLong() {
        return Long.valueOf(millisIdStr());
    }

    /**
     * 附加用户的编码
     *
     * @param code
     * @return
     */
    public static String uniqueNo(String code) {
        Assert.notNull(code, "code cant`t be empty");
        return uniqueNo(code, 2);
    }

    /**
     * 附加用户的编码
     *
     * @param code
     * @param length
     * @return
     */
    public static String uniqueNo(String code, Integer length) {
        Assert.notNull(code, "code cant`t be empty");
        Assert.notNull(length, "length cant`t be empty");
        String prefix = code;
        if (StringUtils.isNotBlank(code)) {
            boolean result = NumericUtil.isNumeric(code);
            if (result) {
                prefix = code.substring(code.length() - length);
            } else {
                String hashCode = String.valueOf(Math.abs(code.hashCode()));
                prefix = hashCode.substring(hashCode.length() - length);
            }
        }
        return uniqueNoWithRandom(prefix, 3);
    }

    /**
     * 符加用户的编码
     *
     * @param prefix
     * @param count
     * @return
     */
    public static String uniqueNoWithRandom(String prefix, Integer count) {
        Assert.notNull(prefix, "prefix cant`t be empty");
        Assert.notNull(count, "count cant`t be empty");
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(prefix)) {
            sb.append(prefix);
        }
        sb.append(System.currentTimeMillis());
        if (ObjectUtils.isNotEmpty(count)) {
            String random = RandomStringUtils.randomNumeric(count);
            sb.append(random);
        }
        return sb.toString();
    }

    /**
     * 手机短信验证码:6位数字
     *
     * @return
     */
    public static String getSmsVerifyCode() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int code = random.nextInt(9);
            sb.append(code);
        }
        return sb.toString();
    }

    /**
     * 生成一个全小写的uuid
     *
     * @return
     */
    public static String getUuid() {
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }

    /**
     * 全大写UUID
     *
     * @return
     */
    public static String getUpperUuid() {
        return getUuid().toUpperCase();
    }

    /**
     * 全小写UUID
     *
     * @return
     */
    public static String getLowerUuid() {
        return getUuid().toLowerCase();
    }

    public static void main(String[] args) {


    }

}
